package com.smarttourism.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smarttourism.common.core.domain.R;
import com.smarttourism.domain.Roomorder;
import com.smarttourism.service.RoomorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
public class RoomOrderController {
    @Autowired
    private RoomorderService roomorderService;
    @GetMapping("/init")
    @PreAuthorize("@ss.hasPermi('services:orders:list')")
    public R<String> initRoomOrderList(){
        roomorderService.InitRoomOrderList();
        return R.ok("订单列表初始化完成");
    }

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('services:orders:list')")

    public R findRoomOrdersList(@RequestParam("rname")String rname, @RequestParam("statusId") Integer statusId,@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Roomorder> roomorderList = roomorderService.RoomOrderList(rname,statusId);
        PageInfo<Roomorder> roomorderPageInfo = new PageInfo<>(roomorderList);
        if (roomorderList!=null){
            return R.ok(roomorderPageInfo);
        }else {
            return R.fail("查询失败");
        }
    }

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('services:orders:add')")
    public R<Integer> addRoomOrder(@RequestBody Roomorder roomorder){
        int i = roomorderService.addRoomOrder(roomorder);
        return R.ok(i);
    }

    @GetMapping("/freeRooms")
    @PreAuthorize("@ss.hasPermi('services:orders:add')")
    public R findFreeRooms(){
        return R.ok(roomorderService.findFreeRooms());
    }

    @DeleteMapping("/deleteOrder")
    @PreAuthorize("@ss.hasPermi('services:orders:add')")
    public R<String> deleteOrder(@RequestParam int id){
        int i = roomorderService.deleteOrderById(id);
        if (i>0){
            return R.ok("删除成功");
        }else return R.fail("删除失败");
    }

    @PutMapping("/updateOrderStatus")
    @PreAuthorize("@ss.hasPermi('services:orders:add')")
    public R<String> updateOrderStatus(@RequestParam int status,@RequestParam int id){
        int i = roomorderService.updateOrderStatus(status, id);
        if (i>0){
            return R.ok("修改成功");
        }else return R.fail("修改失败");
    }

}
